package com.leoman.plz_p.presenter;


import com.leoman.plz_p.interfaces.MainView;
import com.leoman.plz_p.module.api.MainApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.CourseBean;
import com.leoman.plz_p.module.bean.MapData;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.subscriber.ResponseSubscriber;
import com.leoman.plz_p.utils.UserHelper;
import com.leoman.plz_p.utils.authority.AuthorityContext;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class MainPresenter extends BasePresenter<MainView> {

    private MainApi mainApi;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        mainApi=getApi(MainApi.class);
    }

    public void getCourseList(String name, final boolean search){
        mainApi.getCourseList(name,1,Integer.MAX_VALUE).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseBean>> listData) {
                        if (checkListNotNull(listData.data.getRows())){
                            view.obtainCourseList(listData.data.getRows(),search);
                        }else {
                            if (search){
                                view.obtainCourseList(listData.data.getRows(),search);
                            }
                        }
                    }

                });
    }

    public void getRecentStudy(){
        mainApi.getRecentStudy(UserHelper.getToken()).compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> listData) {
                        if (checkDataNotNull(listData))
                        view.obtainRecentStudy(listData.data);
                    }

                    @Override
                    public boolean operationError(BaseData<MapData> data, int status, String message) {
                        if (status==8||message.contains("失效")){
                            view.obtainTokenInvalid();
                        }
                        return super.operationError(data, status, message);
                    }
                });
    }

    public void getAboutUs(){
        mainApi.getAboutUs().compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> listData) {
                        if (checkDataNotNull(listData))
                           view.obtainAboutUs(listData.data.content);
                    }

                });
    }

    public void getHelp(){
        mainApi.getHelp().compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> listData) {
                        if (checkDataNotNull(listData))
                            view.obtainHelp(listData.data.content);
                    }

                });
    }

    @Override
    public void onStart() {
        if (AuthorityContext.getContext().isLoggedIn()){
            getRecentStudy();
        }
    }
}